package com.mondo.financialwallet.presentation.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.StringRes;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mondo.financialwallet.R;

/**
 * 检验密码对话框
 */
public class VerificationPasswordDialog extends Dialog {

    // 谷歌验证密码
    public static Builder googleValidator() {
        return new Builder(
                R.string.title_google_validator,
                R.string.placeholder_google_validator,
                R.string.text_edit_google_validator
        );
    }

    public static Builder login() {
        return new Builder(
                R.string.title_login_password,
                R.string.placeholder_login_password,
                R.string.string_empty
        );
    }

    private EditText mPasswordEditText;
    private View mContentLayout, mProgressLayout;
    private TextView mErrorMessageTextView, mPositiveButton;


    public VerificationPasswordDialog(Context context, Builder builder) {
        super(context);

        ViewGroup root = (ViewGroup) LayoutInflater.from(context).inflate(
                R.layout.dialog_verification_password, null);
        addContentView(root, new LinearLayout.LayoutParams(getDialogWidth(context),
                LinearLayout.LayoutParams.MATCH_PARENT));

        // 标题
        TextView titleTextView = root.findViewById(R.id.dialog_title_text);
        titleTextView.setText(builder.title);

        // 对话框内容布局
        mContentLayout = root.findViewById(R.id.dialog_content_layout);

        // 进度
        mProgressLayout = root.findViewById(R.id.dialog_progress_layout);

        // 密码
        mPasswordEditText = root.findViewById(R.id.dialog_password_edit);
        mPasswordEditText.setHint(builder.placeholder);

        // 错误信息
        mErrorMessageTextView = root.findViewById(R.id.error_message_text_view);

        // 取消
        TextView negativeButton = root.findViewById(R.id.dialog_negative_button);
        negativeButton.setOnClickListener(v -> dismiss());

        // 积极的行为
        mPositiveButton = root.findViewById(R.id.dialog_positive_button);
        mPositiveButton.setOnClickListener(v -> {
            mContentLayout.setVisibility(View.INVISIBLE);
            mProgressLayout.setVisibility(View.VISIBLE);
            builder.positiveListener.onClick(this);
        });

        // 行为
        TextView actionTextView = root.findViewById(R.id.dialog_action_text);
        actionTextView.setText(builder.action);
        actionTextView.setOnClickListener(v -> {
            dismiss();
            builder.actionListener.onClick(v);
        });
    }

    // 获取输入密码
    public String getText(){
        return mPasswordEditText.getText().toString();
    }

    // 错误信息
    public void setErrorMessage(String errorMessage) {
        mProgressLayout.setVisibility(View.GONE);
        mContentLayout.setVisibility(View.VISIBLE);
        mErrorMessageTextView.setText(errorMessage);
    }

    private int getDialogWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return (int) (dm.widthPixels * .85F);
    }


    public interface OnPositiveListener {
        void onClick(VerificationPasswordDialog dialog);
    }


    public static class Builder {
        private @StringRes int title;
        private @StringRes int placeholder;
        private @StringRes int action;
        private OnPositiveListener positiveListener;
        private View.OnClickListener actionListener;

        private Builder(int title, int placeholder, int action) {
            this.title = title;
            this.placeholder = placeholder;
            this.action = action;
        }

        public Builder setPositive(OnPositiveListener listener) {
            this.positiveListener = listener;
            return this;
        }

        public Builder setActionListener(View.OnClickListener listener) {
            this.actionListener = listener;
            return this;
        }

        public VerificationPasswordDialog build(Context context){
            return new VerificationPasswordDialog(context, this);
        }
    }
}
